package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleB2cRetreatCriteria;
import com.javasm.domin.entity.AftersaleB2cChange;
import com.javasm.domin.entity.AftersaleB2cRetreat;
import com.javasm.domin.vo.AftersaleB2cChangeVo;
import com.javasm.domin.vo.AftersaleB2cRetreatVo;
import com.javasm.mapper.AftersaleB2cChangeMapper;
import com.javasm.service.AftersaleB2cChangeService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleB2cChangeTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleB2cChangeServiceImpl extends BaseServiceimpl<AftersaleB2cChange> implements AftersaleB2cChangeService {
    private final AftersaleB2cChangeMapper aftersaleB2cChangeMapper;
    private final AftersaleB2cChangeTransfer aftersaleB2cChangeTransfer;
    @Override
    public PageResult<AftersaleB2cChangeVo> searchPage(AftersaleB2cRetreatCriteria aftersaleB2cRetreatCriteria) {
        //开启分页
        PageHelper.startPage(aftersaleB2cRetreatCriteria.getCurrentPage(),aftersaleB2cRetreatCriteria.getPageSize());
        //条件创建
        LambdaQueryWrapper<AftersaleB2cChange> lambda = new QueryWrapper<AftersaleB2cChange>().lambda();
        if(aftersaleB2cRetreatCriteria.getEstablishId()!=null&&aftersaleB2cRetreatCriteria.getEstablishId()!=""){
            lambda.like(AftersaleB2cChange::getEstablishId, aftersaleB2cRetreatCriteria.getEstablishId());
        }
        if(!StringUtils.isEmpty(aftersaleB2cRetreatCriteria.getEstablishstartTime())){
            lambda.between(AftersaleB2cChange::getEstablishTime, aftersaleB2cRetreatCriteria.getEstablishstartTime(), aftersaleB2cRetreatCriteria.getEstablishendTime());
        }

        if(aftersaleB2cRetreatCriteria.getOrderId()!=null&&aftersaleB2cRetreatCriteria.getOrderId()!=0){
            lambda.eq(AftersaleB2cChange::getOrderId, aftersaleB2cRetreatCriteria.getOrderId());
        }
        if(aftersaleB2cRetreatCriteria.getApproverId()!=null&&aftersaleB2cRetreatCriteria.getApproverId()!=""){
            lambda.like(AftersaleB2cChange::getApproverId, aftersaleB2cRetreatCriteria.getApproverId());
        }
        if(aftersaleB2cRetreatCriteria.getNumberState()!=null&&aftersaleB2cRetreatCriteria.getNumberState()!=0){
            lambda.eq(AftersaleB2cChange::getNumberState, aftersaleB2cRetreatCriteria.getNumberState());
        }
        if(!StringUtils.isEmpty(aftersaleB2cRetreatCriteria.getStartTime())){
            lambda.between(AftersaleB2cChange::getCreateTime, aftersaleB2cRetreatCriteria.getStartTime(), aftersaleB2cRetreatCriteria.getEndTime());
        }

        List<AftersaleB2cChange> aftersaleB2cChanges = aftersaleB2cChangeMapper.selectList(lambda);
        /*获取总条数*/
        PageInfo<AftersaleB2cChange> aftersaleOutPageInfo = new PageInfo<>(aftersaleB2cChanges);
        return new PageResult<AftersaleB2cChangeVo>(aftersaleOutPageInfo.getTotal(),aftersaleB2cChangeTransfer.jotoVo(aftersaleB2cChanges));

    }
}
